package org.car.world.common.result;

import lombok.Data;

import java.io.Serializable;
import java.util.List;

/**
 * JSON返回结果
 */
@Data
public class JsonResult<T> implements Serializable {
    /**
     * 编码
     */
    private Integer code;
    /**
     * 单个对象
     */
    private T obj;
    /**
     * 返回文字信息
     */
    private String msg;
    /**
     * 返回文件名称
     */
    private String filename;
    /**
     * 返回文件添加
     */
    private String id;
    /**
     * 结果集合
     */
    private List<T> data;

    public static <T> JsonResult<T> success(T data) {
        return new JsonResult<T>(0, data, "OK");
    }

    public static <T> JsonResult<T> error(T data) {
        return new JsonResult<T>(1, data, "ERR");
    }

    /**
     * @param data
     * @param flag 标识成功失败
     * @return
     */
    public static <T> JsonResult<T> info(T data, Boolean flag) {
        if (flag)
            return new JsonResult<T>(0, data, "OK");
        else
            return new JsonResult<T>(1, data, "ERR");
    }

    public JsonResult() {
    }

    private JsonResult(int code, T data, String msg) {
        this.code = code;
        this.obj = data;
        this.msg = msg;
        this.filename = filename;
    }

    private JsonResult(int code, String msg) {
        this.code = code;
        this.msg = msg;
        this.filename = filename;
    }

}
